//
//  HotTopicViewController.m
//  Beauty
//
//  Created by Snow on 16/9/14.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "HotTopicViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "ModelOfDetailHotTopic.h"
#import "DetaiHotWindowTableViewCell.h"
#import "ViewControllerOfHotWindow.h"
#import "LoginViewController.h"
@interface HotTopicViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrayOfHotTopic;
@property (nonatomic, strong) NSString *person_id;
@end

@implementation HotTopicViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
}
- (void)handleData
{
    _arrayOfHotTopic = [NSMutableArray array];
    //http://www.wantease.com/mobile1/index.php?act=index&op=hot_topic_info_1_5&class_id=1&key=979742e53b1003df4b0ecce6ba8a1168
    NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=index&op=hot_topic_info_1_5&class_id=%@", _class_id];
    [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"micro_personal"];
        for (NSDictionary *dict in arr) {
            ModelOfDetailHotTopic *model = [[ModelOfDetailHotTopic alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfHotTopic addObject:model];
        }
          [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
    
}
- (void)createView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[DetaiHotWindowTableViewCell class] forCellReuseIdentifier:@"DetaiHotWindowTableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrayOfHotTopic.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetaiHotWindowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetaiHotWindowTableViewCell"];
    ModelOfDetailHotTopic *model = [_arrayOfHotTopic objectAtIndex:indexPath.row];
    [cell.imageOfContent sd_setImageWithURL:[NSURL URLWithString:model.commend_image]];
    [cell.imageOfPerson sd_setImageWithURL:[NSURL URLWithString:model.member_avatar]];
    cell.labelOfName.text = model.member_truename;
    cell.labelOfContent.text = model.commend_message;
    cell.labelOfContent.numberOfLines = 0;
    cell.labelOfLikeCount.text = model.like_count;
    cell.labelOfLikeCount.textColor = [UIColor lightGrayColor];
    [cell.buttonOfContract addTarget:self action:@selector(handleContractPerson2:) forControlEvents:UIControlEventTouchUpInside];
    _person_id = model.personal_id;
    return cell;
}
- (void)handleContractPerson2:(UIButton *)button
{
    //获取UserDefault
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *name = [userDefault objectForKey:@"name"];
    NSString *password = [userDefault objectForKey:@"password"];
    if (name == nil)
    {
        LoginViewController *login = [[LoginViewController alloc] init];
        NSLog(@"%@",name);
        [self presentViewController:login animated:YES completion:^{
            
        }]  ;
    } else {

        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=sns_friend&op=add_follow&key=%@&member_id=%@", password, _person_id];
        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"已关注" message:@"" preferredStyle:UIAlertControllerStyleAlert];
            
            // 出现
            [self presentViewController:alertController animated:YES completion:^{
                
            }];
            // 移除
            [alertController dismissViewControllerAnimated:YES completion:^{
            }];
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ModelOfDetailHotTopic *model = [_arrayOfHotTopic objectAtIndex:indexPath.row];
    ViewControllerOfHotWindow *view = [[ViewControllerOfHotWindow alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    view.person_id = model.personal_id;
    [self.navigationController pushViewController:view animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
